﻿using UnityEngine;
using System.Collections;
using Dict.DataModel;

namespace Room
{
    public class RCAIMine : RCAIBase
    {
        public RCAIMine(RoomCharactor owner)
            : base(owner)
        {
            type = AIType.Mine;
        }

        TableFieldResourcePos minePos;
        RoomCharactor player;
        float r;

        public void Init(TableFieldResourcePos minePos)
        {
            this.minePos = minePos;
            this.r = (float)minePos.r;
            this.player = owner.charactorManager.GetSelfCharactor();
        }

        float distance = 10f;
        float delay = 20f;
        float timer = 20f;

        public override void Tick()
        {
            if (player != null)
            {
                var dis = Vector3.Distance(owner.transform.position, player.transform.position);

                if (dis < distance)
                {
                    timer += Time.deltaTime;
                    if (timer >= delay)
                    {
                        timer = 0f;
                        var room = Room.RoomManager.currentRoom as RoomField;
                        if (room != null)
                        {
                            var netManager = (RoomNetField)room.netManager;
                            netManager.SendResourceCheck(minePos.posId);
                        }
                    }
                }

                if (dis <= r)
                {
                    var ui = owner.ui as RCUIMine;
                    ui.ShowButton();
                }
                else
                {
                    owner.ui.HideButton();
                }

            }
        }
    }
}

